// pages/category/index.js
import { request } from '../../request/index'
Page({
  /**
   * 页面的初始数据
   */
  data: {
    shoppingList:[],
    rightShoppingList:[],
    countIndex:0,
    scrollIndex:0
  },
  lists:[],
  /** * 生命周期函数--监听页面加载 */
  onLoad: function (options) {
    const lists = wx.getStorageSync("lists");

    if(!lists){
      this.getShopList()
    }else{
      if(Date.now()-lists.time > 1000*10){
        this.getShopList()
      }else{
        this.lists = lists.data
        let rightlist = this.lists[0]
        this.setData({
          shoppingList:this.lists,
          rightShoppingList:rightlist
        })
      }
    }
      
    
  },
  getShopList(){
    request({
      url:'/categories'
    }).then(resoult => {
      let list = resoult.data.message
      this.lists = list
      
      wx.setStorageSync("lists", {time:Date.now(),data:this.lists});
      let rightlist = this.lists[0]
        
      this.setData({
        shoppingList:this.lists,
        rightShoppingList:rightlist
      })
    })
  },
  /*定义左侧列表点击事件 */
  handleClick(e){
    let {index} = e.currentTarget.dataset
    let rightlist = this.lists[index]
    this.setData({
      countIndex:index,
      rightShoppingList:rightlist,
      scrollIndex:0
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})